package handler

import (
	"context"
	"easy-note/gateway/utils/resp"
	"easy-note/note/kitex_gen/note"
	noteClient "easy-note/note/kitex_gen/note/note"
	"fmt"
	"github.com/cloudwego/hertz/pkg/app"
	"strconv"
)

type NoteHandler struct{}

var NoteClient noteClient.Client

func (noteHandler *NoteHandler) GetNoteById(ctx context.Context, c *app.RequestContext) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		resp.BadRequestResponse(c, 1, "请求参数错误")
	}
	userId := c.GetInt32("userId")
	result, err := NoteClient.GetNoteById(ctx, &note.ReqGetNoteById{Id: int32(id), UserId: userId})
	if err != nil {
		resp.BadRequestResponse(c, 7, "查找错误")
		return
	}
	if result == nil {
		resp.NotFountResponse(c, "找不到该笔记")
		return
	}
	resp.SuccessResponse(c, "请求成功", result)
}

func (noteHandler *NoteHandler) ListNote(ctx context.Context, c *app.RequestContext) {
	page, err := strconv.Atoi(c.Query("page"))
	if err != nil {
		resp.BadRequestResponse(c, 1, "请求参数错误")
		return
	}
	size, err := strconv.Atoi(c.Query("size"))
	if err != nil {
		resp.BadRequestResponse(c, 1, "请求参数错误")
		return
	}
	userId := c.GetInt32("userId")
	var total = int64(0)
	notes, err := NoteClient.ListNote(ctx, &note.ReqListNote{
		Page:   int32(page),
		Size:   int32(size),
		UserId: userId,
		Total:  total,
	})
	if err != nil {
		resp.BadRequestResponse(c, 7, "查找错误")
		return
	}
	if notes == nil {
		resp.NotFountResponse(c, "找不到笔记")
		return
	}
	resp.SuccessResponse(c, fmt.Sprintf("找到%d篇", notes.Total), notes)
}

func (noteHandler *NoteHandler) UpdateNote(ctx context.Context, c *app.RequestContext) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		resp.BadRequestResponse(c, 1, "请求参数错误")
	}
	var newNote struct {
		Name, Note string
	}
	err = c.BindAndValidate(&newNote)
	if err != nil {
		resp.BadRequestResponse(c, 1, "请求参数错误")
	}
	userId := c.GetInt32("userId")
	result, err := NoteClient.UpdateNote(ctx, &note.ReqUpdateNote{
		Name:   newNote.Name,
		Note:   newNote.Note,
		UserId: userId,
		Id:     int32(id),
	})
	if err != nil {
		resp.BadRequestResponse(c, 7, "更新错误")
		return
	}
	if !result.GetIsSuccess() {
		resp.BadRequestResponse(c, 5, "修改失败")
		return
	}
	resp.SuccessResponse(c, "修改成功", nil)
}

func (noteHandler *NoteHandler) DeleteNote(ctx context.Context, c *app.RequestContext) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		resp.BadRequestResponse(c, 1, "请求参数错误")
	}
	userId := c.GetInt32("userId")
	result, err := NoteClient.DeleteNote(ctx, &note.ReqDeleteNote{
		Id:     int32(id),
		UserId: userId,
	})
	if err != nil {
		resp.BadRequestResponse(c, 7, "删除错误")
		return
	}
	if !result.GetIsSuccess() {
		resp.BadRequestResponse(c, 6, "删除失败")
	}
	resp.SuccessResponse(c, "删除成功", nil)
}

func (noteHandler *NoteHandler) CreateNote(ctx context.Context, c *app.RequestContext) {
	var newNote struct {
		Name, Note string
	}
	err := c.BindAndValidate(&newNote)
	if err != nil {
		resp.BadRequestResponse(c, 1, "请求参数错误")
	}
	userId := c.GetInt32("userId")
	result, err := NoteClient.InsertNote(ctx, &note.ReqInsertNote{
		Name:   newNote.Name,
		Note:   newNote.Note,
		UserId: userId,
	})
	if err != nil {
		resp.BadRequestResponse(c, 7, "创建错误")
		return
	}
	if !result.GetIsSuccess() {
		resp.BadRequestResponse(c, 5, "创建失败")
	}
	resp.SuccessResponse(c, "创建成功", nil)
}
